package com.antipov.odp.pojo;

import com.antipov.odp.exceptions.InvalidIntParameter;
import com.antipov.odp.exceptions.UnluckyParameterException;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User: Andrew Antipov
 * Date: 18.02.14
 * Time: 21:41
 */
public class A implements I{

	public static final int ID = 1;

	protected int[] array;
	protected int x;

	public A(int[] array, int x) {
		this.array = array;
		this.x = x;
	}

	@Override
	public int foo() {
		return x * array.length;
	}

	@Override
	public int bar(int z) throws UnluckyParameterException {
		if(z == 0) {
			throw new InvalidIntParameter("A::bar", null, z);
		}
		if(z == 13) {
			throw new UnluckyParameterException();
		}
		return array.length / z;
	}

	public int[] getArray() {
		return array;
	}

	public void setArray(int[] array) {
		this.array = array;
	}

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	@Override
	public String toString() {
		String res = "";
		res += ID;
		res += ' ';
		res += internalStateToString();
		return res;
	}

	protected String internalStateToString() {
		String res = "";
		res += array.length;
		res += ' ';
		for(int i = 0; i < array.length; i++) {
			res += array[i];
			res += ' ';
		}
		res += x;
		return res;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if(obj == null || !(obj instanceof A)) {
			return false;
		}
		A a = (A) obj;
		return Arrays.equals(array, a.array) && x == a.x;
	}

	@Override
	public int hashCode() {
		return x ^ Arrays.hashCode(array);
	}
}
